﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Net;
using System.Net.Security;
using System.IO;
using System.Security.Cryptography.X509Certificates;

namespace TheSchedulator
{
    public class HttpRequest
    {
        private HttpWebRequest m_request;
        private string m_streamResponse = null;

        private HttpMethod m_requestMethod;
        private string m_responseQuery;

        public HttpRequest(HttpMethod _Method, Uri _Url,string _Referer,string _Host,CookieContainer _Cookies)
        {
            // Create the request
            m_request = (HttpWebRequest)HttpWebRequest.Create(_Url);

            // Creating the HEAD
            this.m_requestMethod = _Method;
            this.m_request.Method = m_requestMethod.ToString();
            this.m_request.ContentType = "application/x-www-form-urlencoded";
            this.m_request.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            this.m_request.KeepAlive = true;
            this.m_request.Host = _Host;
            this.m_request.Referer = _Referer;
            this.m_request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
            this.m_request.CookieContainer = _Cookies;

            #region SSL CertificateValidation
            ServicePointManager.ServerCertificateValidationCallback =
                        delegate(
                            object sender1,
                            X509Certificate certificate,
                            X509Chain chain,
                            SslPolicyErrors sslPolicyErrors)
                        {
                            return true;
                        };
            #endregion
        }

        public HttpRequest(HttpMethod _Method, Uri _Url)
            : base()
        {
            // Create the request
            m_request = (HttpWebRequest)HttpWebRequest.Create(_Url);

            // Creating the HEAD
            this.m_requestMethod = _Method;
            this.m_request.Method = m_requestMethod.ToString();
            this.m_request.ContentType = "application/x-www-form-urlencoded";
            this.m_request.Accept = "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            this.m_request.KeepAlive = true;
            this.m_request.UserAgent = "User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64; rv:18.0) Gecko/20100101 Firefox/18.0";
            this.m_request.CookieContainer = new CookieContainer();

            #region SSL CertificateValidation
            ServicePointManager.ServerCertificateValidationCallback =
                        delegate(
                            object sender1,
                            X509Certificate certificate,
                            X509Chain chain,
                            SslPolicyErrors sslPolicyErrors)
                        {
                            return true;
                        };
            #endregion
        }

        /// <summary>
        /// Use for a POST Method
        /// </summary>
        /// <param name="_Data"></param>
        /// <returns></returns>
        public HttpWebResponse SendRequest(string _Data)
        {
      
            // Creating the message of the body
            ASCIIEncoding encoding = new ASCIIEncoding();

            byte[] data = encoding.GetBytes(_Data);

            // Write the stream to the server
            using (Stream newStream = this.m_request.GetRequestStream())
            {
                newStream.Write(data, 0, data.Length);
            }

            return this.GetRequestResponse();
          
        }

        /// <summary>
        /// Use this method for a GET Method
        /// </summary>
        /// <returns></returns>
        public HttpWebResponse SendRequest()
        {

            return this.GetRequestResponse();

        }

        private HttpWebResponse GetRequestResponse()
        {
            // Get the response from the server, take the content of the message and put in m_streamResponse
            HttpWebResponse response = (HttpWebResponse)this.m_request.GetResponse();

            using (StreamReader reader = new StreamReader(this.m_request.GetResponse().GetResponseStream()))
            {

                this.m_streamResponse = reader.ReadToEnd();
                this.m_responseQuery = response.ResponseUri.Query;
            }
            return response;
        }



        #region Accesseurs
        public string ResponseStream
        {
            get { return this.m_streamResponse; }
        }
        public CookieContainer Cookies
        {
            get { return this.m_request.CookieContainer; }
        }
        public string ResponseQuery
        {
            get { return this.m_responseQuery; }
        }
        #endregion
    }
}
